package com.cy.store.service.impl;

import com.cy.store.entity.User;
import com.cy.store.mapper.UserMapper;
import com.cy.store.service.IUserService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void reg(User user) {
        //判断用户名是否被注册
        User result = userMapper.findByUsername(user.getUsername());
        if (result!=null){
            throw new UsernameDuplicatedException("该用户已注册");
        }
        user.setIsDelete(0);
        user.setCreatedUser(user.getUsername());
        user.setModifiedUser(user.getUsername());
        Date date = new Date();
        user.setCreatedTime(date);
        user.setModifiedTime(date);
        //使用UUID作为盐值
        String salt = UUID.randomUUID().toString().toUpperCase();
        user.setSalt(salt);
        String oldPassword = user.getPassword();
        String password = getMD5Password(oldPassword, salt);
        user.setPassword(password);

        Integer row = userMapper.insert(user);
        System.out.println("Ok");
        if (row!=1){
            throw new InsertException("在用户注册过程中产生未知异常");
        }

    }

    @Override
    public User login(String username,String password) {
        //通过username查询用户
        User result = userMapper.findByUsername(username);
        //判断是否存在输入用户数据
        if (result==null){
            throw new UserNotFoundException("用户数据不存在");
        }
        //把用户输入密码作相同加密后与数据库密码作比较
        String salt = result.getSalt();
        String oldPassword = result.getPassword();
        String md5Password = getMD5Password(password, salt);
        if (!oldPassword.equals(md5Password)){
            throw new PasswordNotMatchException("用户密码不匹配");
        }
        if (result.getIsDelete()!=0){
            throw new UserNotFoundException("用户数据不存在");
        }
        //返回user,便利后续操作
        User user = new User();
        user.setUid(result.getUid());
        user.setUsername(result.getUsername());
        user.setAvatar(result.getAvatar());
        return user;
    }

    @Override
    public void updatePasswordByUid(Integer uid, String username,String oldPassword,String newPassword) {
        User result = userMapper.findByUid(uid);
        //如果用户为null,抛出用户数据不存在异常
        if (result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }
        //比较输入密码是否匹配
        String oldMd5Password = getMD5Password(oldPassword, result.getSalt());
        if (!oldMd5Password.equals(result.getPassword())){
            throw new PasswordNotMatchException("用户密码不匹配");
        }
        //修改密码存入数据库
        String md5Password = getMD5Password(newPassword, result.getSalt());
        Integer row = userMapper.updatePasswordByUid(uid, md5Password, username, new Date());
        if (row!=1){
            throw new UpdateException("更新数据时产生未知异常");
        }
    }

    @Override
    public User findByUid(Integer uid) {
        User result = userMapper.findByUid(uid);
        if (result==null){
            throw new UserNotFoundException("用户数据不存在");
        }
        User user = new User();
        user.setUsername(result.getUsername());
        user.setPhone(result.getPhone());
        user.setEmail(result.getEmail());
        user.setGender(result.getGender());
        return user;
    }

    @Override
    public void changeInfo(Integer uid,String username,User _user) {
        User result = userMapper.findByUid(uid);
        if (result==null){
            throw new UserNotFoundException("用户数据不存在");
        }
        //用户存在，允许修改
        User user = new User();
        user.setUid(uid);
        user.setPhone(_user.getPhone());
        user.setEmail(_user.getEmail());
        user.setGender(_user.getGender());
        user.setModifiedUser(username);
        user.setModifiedTime(new Date());
        Integer rows = userMapper.updateInfoByUid(user);
        if (rows!=1){
            throw new UpdateException("更新数据时产生未知异常");
        }

    }

    @Override
    public void changeAvatar(Integer uid, String username, String avatar) {
        User result = userMapper.findByUid(uid);
        if (result==null||result.getIsDelete().equals(1)){
            throw new UserNotFoundException("用户数据不存在");
        }
        //更新头像
        Integer roes = userMapper.updateAvatarByUid(uid, avatar, username, new Date());
        if (roes!=1){
            throw new UpdateException("头像信息更新时产生未知异常");
        }
    }

    /** 对密码进行md5加密处理 */
    private String getMD5Password(String password,String salt){
        for (int i = 0; i < 3; i++) {
             password = DigestUtils.md5DigestAsHex(((salt + password + salt).getBytes())).toUpperCase();
        }
        return password;
    }
}
